package cn.ai.boot.config;

import java.util.Locale;

public class LocaleUtils {
    private static final ThreadLocal<Locale> LOCALE_THREAD_LOCAL = new InheritableThreadLocal();

    public static void set(Locale lang) {
        LOCALE_THREAD_LOCAL.set(lang);
    }

    public static Locale get() {
        return (Locale)LOCALE_THREAD_LOCAL.get();
    }

    public static Locale getOrDefault(Locale defaultLocale) {
        Locale locale = (Locale)LOCALE_THREAD_LOCAL.get();
        return locale == null ? defaultLocale : locale;
    }

    public static Locale getOrDefault() {
        Locale locale = (Locale)LOCALE_THREAD_LOCAL.get();
        return locale == null ? Locale.getDefault() : locale;
    }

    public static void remove() {
        LOCALE_THREAD_LOCAL.remove();
    }
}
